package cmpe451.group7.ourcity.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cmpe451.group7.ourcity.dao.BadgeDAO;
import cmpe451.group7.ourcity.entity.Badge;
import cmpe451.group7.ourcity.entity.User;
import cmpe451.group7.ourcity.util.Logger;

@Repository("badgeDAO")
public class BadgeDAOImpl extends GenericDAOImpl<Badge> implements BadgeDAO {

	private static final long serialVersionUID = 1L;

	@Override
	@Transactional
	public List<Badge> findAll() {
		return super.findAll(Badge.class);
	}

	@Override
	@Transactional
	public Badge findByID(long id) {
		return super.findByID(Badge.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Badge> getCommentBadges() {
		try {

			Session session = this.getCurrentSession();

			String hql = "select a from Badge a " + "where "
					+ "commentOrPlace like 0 " + "group by a";

			Query query = session.createQuery(hql);

			List<Badge> badges = query.list();

			return badges;
		} catch (Exception e) {
			Logger.log("Error searching places");
			Logger.log(e);
			return null;
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public List<Badge> getPlaceBadges() {
		try {

			Session session = this.getCurrentSession();

			String hql = "select a from Badge a " + "where "
					+ "commentOrPlace like 1 " + "group by a";

			Query query = session.createQuery(hql);

			List<Badge> badges = query.list();

			return badges;
		} catch (Exception e) {
			Logger.log("Error searching places");
			Logger.log(e);
			return null;
		}

	}

	@Override
	@Transactional
	public Badge insertNewBadge(String badgeName, int commentOrPlace, int count) {

		Badge badge = new Badge();
		Transaction transaction = null;
		try {

			Session session = this.getCurrentSession();
			transaction = session.beginTransaction();

			badge.setBadgeName(badgeName);
			badge.setCommentOrPlace(commentOrPlace);
			badge.setBadgeCount(count);

			session.save(badge);
			//session.flush();

			transaction.commit();

			return badge;
		} catch (Exception e) {
			Logger.log("Error while adding a new badge");
			Logger.log(e);
			transaction.rollback();
		}

		return null;

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public Set<Badge> getUserBadges(long userID) {
		try {
			Session session = this.getCurrentSession();

			String hql = "select a from User a " + "where "
					+ "userID like :us " + "group by a";

			Query query = session.createQuery(hql);
			query.setParameter("us", userID);

			List<User> users = query.list();

			Set<Badge> badges = users.get(0).getBadges();

			return badges;
		} catch (Exception e) {
			Logger.log("Error searching places");
			Logger.log(e);
			return null;
		}
	}

	@Override
	@Transactional
	public List<Badge> getUserBadgesList(long userID) {
		BadgeDAO badgeDAO = new BadgeDAOImpl();
		Set<Badge> badges = badgeDAO.getUserBadges(userID);
		List<Badge> badgeList = new ArrayList<Badge>();
		badgeList.addAll(badges);

		return badgeList;
	}

}
